"""
扫货模式选择对话框
"""
from PyQt6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QPushButton, 
                           QLabel, QFrame)
from PyQt6.QtCore import Qt, pyqtSignal
from biz.script_task_processor import ScanMode

class ScanModeDialog(QDialog):
    """扫货模式选择对话框"""
    
    # 定义信号，返回选择的扫货模式
    mode_selected = pyqtSignal(object)  # 修改为传递对象而不是整数
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("选择扫货模式")
        self.setMinimumWidth(400)
        self.setMinimumHeight(200)
        self.setWindowFlags(Qt.WindowType.Dialog | Qt.WindowType.WindowCloseButtonHint)
        self.setup_ui()
        
    def setup_ui(self):
        """初始化UI"""
        layout = QVBoxLayout(self)
        layout.setContentsMargins(20, 20, 20, 20)
        layout.setSpacing(20)
        
        # 标题
        title_label = QLabel("请选择扫货模式")
        title_label.setStyleSheet("""
            QLabel {
                font-size: 16px;
                font-weight: bold;
                color: #303133;
            }
        """)
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(title_label)
        
        # 按钮容器
        button_frame = QFrame()
        button_frame.setFrameShape(QFrame.Shape.NoFrame)
        button_layout = QHBoxLayout(button_frame)
        button_layout.setContentsMargins(10, 10, 10, 10)
        button_layout.setSpacing(20)
        
        # 动态扫货按钮
        dynamic_btn = QPushButton("动态扫货")
        dynamic_btn.setMinimumHeight(60)
        dynamic_btn.setStyleSheet("""
            QPushButton {
                background-color: #409eff;
                color: white;
                border: none;
                border-radius: 4px;
                font-size: 14px;
                font-weight: bold;
                padding: 10px;
            }
            QPushButton:hover {
                background-color: #66b1ff;
            }
            QPushButton:pressed {
                background-color: #3a8ee6;
            }
        """)
        dynamic_btn.clicked.connect(self.on_dynamic_selected)
        
        # 轮询扫货按钮
        polling_btn = QPushButton("轮询扫货")
        polling_btn.setMinimumHeight(60)
        polling_btn.setStyleSheet("""
            QPushButton {
                background-color: #67c23a;
                color: white;
                border: none;
                border-radius: 4px;
                font-size: 14px;
                font-weight: bold;
                padding: 10px;
            }
            QPushButton:hover {
                background-color: #85ce61;
            }
            QPushButton:pressed {
                background-color: #5daf34;
            }
        """)
        polling_btn.clicked.connect(self.on_polling_selected)
        
        # 添加按钮到布局
        button_layout.addWidget(dynamic_btn)
        button_layout.addWidget(polling_btn)
        
        layout.addWidget(button_frame)
        
        # 说明文本
        desc_label = QLabel("动态扫货：实时扫描，适合快速响应\n轮询扫货：定时轮询，减少API调用次数")
        desc_label.setStyleSheet("""
            QLabel {
                font-size: 12px;
                color: #909399;
            }
        """)
        desc_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(desc_label)
        
    def on_dynamic_selected(self):
        """选择动态扫货模式"""
        self.mode_selected.emit(ScanMode.DYNAMIC)
        self.accept()
        
    def on_polling_selected(self):
        """选择轮询扫货模式"""
        self.mode_selected.emit(ScanMode.POLLING)
        self.accept()
